/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;
//import java.util.logging.Level;
//import java.util.logging.Logger;

/**
 * TransferAction are used to move an item from on owners inventory to another
 * It can be used to move a basic item (i.e. cone,sign,etc) from a cars inventory to
 * the world and back
 * @author zamille
 */
public class TransferAction extends Action
{
    Item reciever, moved;

    /**
     *
     * @param movedItem
     * @param recieverOfItem
     */
    public TransferAction(Item movedItem, Item recieverOfItem)
    {
        super();
        reciever = recieverOfItem;
        moved = movedItem;
        super.messageType = Message.TRANSFER_MESSAGE;
    }

    /**
     * 
     * @param actor
     * @param movedItem
     * @param recieverOfItem
     */
    public TransferAction(Item actor, Item movedItem, Item recieverOfItem)
    {
        super(actor);
        reciever = recieverOfItem;
        moved = movedItem;
        super.messageType = Message.TRANSFER_MESSAGE;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public TransferAction(ByteBuffer bytes, Vector<Item> items)
    {
        fromByte(bytes,items);
        super.messageType = Message.TRANSFER_MESSAGE;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    TransferAction(String substring, Vector<Item> itemList) {
        fromSaveString(substring, itemList);
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this methods completes
     */
    private ByteBuffer fromByte(ByteBuffer bytes, Vector<Item> items) {
        bytes = super.fromBytes(bytes,items);   //it uses the inherited method to build
        moved = ByteChanger.getItem(bytes, items, 0);  //the inherited members from the ByteBuffer
        reciever = ByteChanger.getItem(bytes, items, 4);
        try {
            bytes = ByteChanger.truncate(bytes, 8);
        } catch (Exception ex) {

        }
        return bytes;
    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = super.toByte();   //it uses the inherited method to put
        buf.putInt(ByteChanger.itemToByte(moved));  //the inherited members in the ByteBuffer
        buf.putInt(ByteChanger.itemToByte(reciever));
        return buf;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    @Override
    public String toSaveString()
    {
        String text = super.toSaveString();   //it uses the inherited method to put
        text += ByteChanger.itemToByte(moved)  + "\t";  //the inherited members in the Save String
        text += ByteChanger.itemToByte(reciever)  + "\t";
        return text;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    @Override
    protected String fromSaveString(String text, Vector<Item> items)
    {
        text = super.fromSaveString(text, items);   //it uses the inherited method to build
        String substring = ByteChanger.getNextValue(text);  //the inherited members from the Save String
        moved = ByteChanger.getItem(substring, items);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        reciever = ByteChanger.getItem(substring, items);

        text = ByteChanger.incrementString(text);
        return text;
    }
}
